import { createRouter, createWebHistory } from 'vue-router'
import { useUserStore } from '@/stores/user' // 引入 Pinia 用户存储
import ManagerHome from '@/views/ManagerHome.vue'
import LoginPage from '@/views/LoginPage.vue'
import PendingApprovalTable from '@/views/manager/PendingApprovalTable.vue'
import ApprovedWorkTable from '@/views/manager/ApprovedworkTable.vue'
import DepartmentPerformance from '@/views/manager/DepartmentPerformance.vue'
import TargetList from '@/views/manager/TargetList.vue'
import NotificationList from '@/views/manager/NotificationList.vue'

import AdminHome from '@/views/AdminHome.vue'
import UserManagement from '@/views/admin/UserManagement.vue'
import DepartmentManagement from '@/views/admin/DepartmentManagement.vue'
import SystemSettings from '@/views/admin/SystemSettings.vue'
import DataExport from '@/views/admin/DataExport.vue'
import NotificationCenter from '@/views/admin/NotificationCenter.vue'

import EmployeeHome from '@/views/EmployeeHome.vue'
import NotificationPage from '@/views/employee/NotificationPage.vue'
import PerformanceStatistics from '@/views/employee/PerformanceStatistics.vue'
import TargetManagement from '@/views/employee/TargetManagement.vue'
import WorkInput from '@/views/employee/WorkInput.vue'
import WorkList from '@/views/employee/WorkList.vue'

import UserAvatar from '@/views/User/UserAvatar.vue'
import UserPassword from '@/views/User/UserPassword.vue'
import UserProfile from '@/views/User/UserProfile.vue'

const routes = [
  { path: '/login', component: LoginPage },
  {
    path: '/manager',
    component: ManagerHome,
    redirect: '/manager/pending-approval',
    children: [
      { path: '/manager/pending-approval', component: PendingApprovalTable },
      { path: '/manager/approved-work', component: ApprovedWorkTable },
      { path: '/manager/department-performance', component: DepartmentPerformance },
      { path: '/manager/target', component: TargetList },
      { path: '/manager/notification', component: NotificationList },
      { path: '/manager/profile', component: UserProfile },
      { path: '/manager/avatar', component: UserAvatar},
      { path: '/manager/repassword',component: UserPassword}
    ],
    meta: { requiresManager: true } // 只有 manager 身份能访问
  },
  { path: '/', redirect: '/login' }, // 主页跳转到 login

  // 管理员路由配置
  {
    path: '/admin',
    component: AdminHome,
    redirect: '/admin/user-management',
    children: [
      { path: '/admin/user-management', component: UserManagement },
      { path: '/admin/department-management', component: DepartmentManagement },
      { path: '/admin/system-settings', component: SystemSettings },
      { path: '/admin/data-export', component: DataExport },
      { path: '/admin/notification', component: NotificationCenter },
      { path: '/admin/profile', component: UserProfile },
      { path: '/admin/avatar', component: UserAvatar},
      { path: '/admin/repassword',component: UserPassword}
    ],
    meta: { requiresAdmin: true }
  },

    // 雇员路由配置
    {
    path: '/employee',
    component: EmployeeHome,
    redirect: '/employee/work-list',
    children: [
      { path: '/employee/notification', component: NotificationPage,},
      { path: '/employee/performance',component: PerformanceStatistics,},
      { path: '/employee/target',component: TargetManagement,},
      { path: '/employee/work-input', component: WorkInput,},
      { path: '/employee/work-list',component: WorkList,},
      { path: '/employee/profile', component: UserProfile ,},
      { path: '/employee/avatar', component: UserAvatar,},
      { path: '/employee/repassword',component: UserPassword,}
    ],
      meta: { requiresEmployee: true }
    },
]

const router = createRouter({
  history: createWebHistory(),
  routes
})

import { ElMessage } from 'element-plus'

router.beforeEach((to, from, next) => {
  const userStore = useUserStore() // 访问 Pinia 状态

  // 公共路由（无需身份验证）
  if (to.path === '/login') {
    return next()
  }

  // 检查用户是否登录
  if (!userStore.role) {
    ElMessage.error("用户信息失效，请重新登录！")
    return next('/login')
  }

  // 身份验证逻辑
  if (to.meta.requiresManager && userStore.role !== 'manager') {
    ElMessage.error('无权限访问该页面')
    return next('/login')
  }

  if (to.meta.requiresAdmin && userStore.role !== 'admin') {
    ElMessage.error('无权限访问该页面')
    return next('/login')
  }

  if (to.meta.requiresEmployee && userStore.role !== 'employee') {
    ElMessage.error('无权限访问该页面')
    return next('/login')
  }

  // 其他情况，继续路由
  next()
})


export default router
